Strict

Import mojo.app
Import mojo.graphics
Import vector2d
Import ball
Import diddy

Function Main:Int()
       New BlueScreen
End

Class BlueScreen Extends DiddyApp
	Field ballsList:ArrayList<Ball> = New ArrayList<Ball>
	Field numBalls:int = 10
	
	
 	Method OnCreate:Int()
		Super.OnCreate()
		GenerateBalls()
        return 0
    End       

    Method OnUpdate:Int()
		For Local ndx:Int=0 Until numBalls
			Local tempBall:Ball = ballsList.Get(ndx)
	    	tempBall.Update()
		next
		checkCollisions()
    	return 0
    End

    Method OnRender:Int()
    	Cls 0,0,200
   		For Local ndx:Int=0 Until numBalls
			Local tempBall:Ball = ballsList.Get(ndx)
	    	tempBall.Draw()
		next

    	Return 0
    End
	
	Method GenerateBalls:void()
		ballsList.Clear()
		For Local ndx:Int=0 Until numBalls
			Local tempBall:Ball = new Ball(Rnd(SCREEN_WIDTH), Rnd(SCREEN_HEIGHT), Rnd(20)+20, 0.1)
			tempBall.velocity.set(Rnd(10), Rnd(10))
		   	ballsList.Add(tempBall)
		next
	End
	
	Method checkCollisions:void()
		ballsList.Sort()

		' Check for collision with walls
		For Local ndx:Int=0 Until numBalls
		'	System.out.println("Ball #" + i + ": " + (ball1.position.getX() - ball1.getRadius()))
			Local ball1:Ball = ballsList.Get(ndx)
			
			if (ball1.position.getX() - ball1.getRadius() < 0)			
				ball1.position.setX(ball1.getRadius()) ' Place ball against edge
				ball1.velocity.setX(-(ball1.velocity.getX() * Ball.restitution)) ' Reverse direction and account for friction
				ball1.velocity.setY(ball1.velocity.getY() * Ball.restitution)
			
			elseif (ball1.position.getX() + ball1.getRadius() > SCREEN_WIDTH) ' Right Wall
			
				ball1.position.setX(SCREEN_WIDTH - ball1.getRadius())		' Place ball against edge
				ball1.velocity.setX(-(ball1.velocity.getX() * Ball.restitution)) ' Reverse direction and account for friction
				ball1.velocity.setY((ball1.velocity.getY() * Ball.restitution))
			endif

			if (ball1.position.getY() - ball1.getRadius() < 0)				' Top Wall
				ball1.position.setY(ball1.getRadius())				' Place ball against edge
				ball1.velocity.setY(-(ball1.velocity.getY() * Ball.restitution)) ' Reverse direction and account for friction
				ball1.velocity.setX((ball1.velocity.getX() * Ball.restitution))			
			elseif (ball1.position.getY() + ball1.getRadius() > SCREEN_HEIGHT) ' Bottom Wall	
				ball1.position.setY(SCREEN_HEIGHT - ball1.getRadius())		' Place ball against edge
				ball1.velocity.setY(-(ball1.velocity.getY() * Ball.restitution))    ' Reverse direction and account for friction
				ball1.velocity.setX((ball1.velocity.getX() * Ball.restitution))
			endif

			' Ball to Ball collision
			For Local ndx2:Int=ndx+1 Until numBalls	
				Local ball2:Ball = ballsList.Get(ndx2)	
				if ((ball1.position.getX() + ball1.getRadius()) < (ball2.position.getX() - ball2.getRadius()))
						exit
				endif

				if((ball1.position.getY() + ball1.getRadius()) < (ball2.position.getY() - ball2.getRadius()) or
				   (ball2.position.getY() + ball2.getRadius()) < (ball1.position.getY() - ball1.getRadius()))
				   		Continue
				endif
				
				ball1.resolveCollision(ball2)
			next
		next
	End
	
	

End
